package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.annotation.InsertAutoFill;
import com.sky.annotation.UpdateAutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * AutoFillAspect
 *
 * @author liliudong
 * @version 1.0
 * @description 公共字段注入切面类
 * @date 2023/6/8 9:40
 */
@Component
@Aspect
@Slf4j
public class AutoFillAspect {

    /**
     * 切点
     */
    @Pointcut("@annotation(com.sky.annotation.AutoFill) || " +
            "@annotation(com.sky.annotation.InsertAutoFill) || " +
            "@annotation(com.sky.annotation.UpdateAutoFill)")
    public void pt() {
    }

    /**
     * 前置通知方法
     *
     * @param joinPoint
     */
    @Before("pt()")
    public void autoFill(JoinPoint joinPoint) {
        log.info("-----------公共字段注入切面类切入成功-----------");

        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0) {
            return;
        }

        Object entity = args[0];
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        try {
            Long currentId = BaseContext.getCurrentId();
            LocalDateTime now = LocalDateTime.now();
            Class<?> clazz = entity.getClass();

            if (method.isAnnotationPresent(InsertAutoFill.class)) {
                autoInsert(entity, currentId, now, clazz);
            }
            if (method.isAnnotationPresent(UpdateAutoFill.class)) {
                autoUpdate(entity, currentId, now, clazz);
            }
            if (method.isAnnotationPresent(AutoFill.class)) {
                AutoFill autoFill = method.getAnnotation(AutoFill.class);
                OperationType operationType = autoFill.value();
                if (OperationType.INSERT.equals(operationType)) {
                    autoUpdate(entity, currentId, now, clazz);
                }
                if (OperationType.UPDATE == operationType) {
                    autoInsert(entity, currentId, now, clazz);
                }
            }
        } catch (Exception e) {
            log.error("------公共字段自动赋值失败-------", e);
        }

    }

    /**
     * 自动新增赋值
     *
     * @param entity
     * @param currentId
     * @param now
     * @param clazz
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    private void autoInsert(Object entity, Long currentId, LocalDateTime now, Class<?> clazz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Method setUpdateTime = clazz.getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
        Method setUpdateUser = clazz.getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
        setUpdateTime.invoke(entity, now);
        setUpdateUser.invoke(entity, currentId);
    }

    /**
     * 自动更新赋值
     *
     * @param entity
     * @param currentId
     * @param now
     * @param clazz
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    private void autoUpdate(Object entity, Long currentId, LocalDateTime now, Class<?> clazz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Method setCreateTime = clazz.getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
        Method setCreateUser = clazz.getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
        Method setUpdateTime = clazz.getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
        Method setUpdateUser = clazz.getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
        setCreateTime.invoke(entity, now);
        setCreateUser.invoke(entity, currentId);
        setUpdateTime.invoke(entity, now);
        setUpdateUser.invoke(entity, currentId);
    }
}
